﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace chungcu
{
    public partial class thongtinphong : Form
    {
        private SqlConnection conn;
        private DataTable dt = new DataTable("phong");
        private DataTable dt1 = new DataTable("phong2");
        private SqlDataAdapter da = new SqlDataAdapter();
        private void connect()
        {
            String cn = "Data source=(local);Initial Catalog=Quanlychungcu;Integrated Security = true";
            try
            {
                conn = new SqlConnection(cn);
                conn.Open();
            }
            catch(SqlException ex) {
                Console.WriteLine(ex.Message);
                MessageBox.Show("khong ket noi duoc CSDL");
            }
        }
        private void disconect()
        {
            conn.Close();
            conn.Dispose();
            conn = null;
        }
        public thongtinphong()
        {
            InitializeComponent();
        }

        private void thongtinphong_Load(object sender, EventArgs e)
        {
            connect();
            getdata();
        }
        private void getdata()
        {
            SqlCommand command = new SqlCommand();
            command.Connection = conn;
            command.CommandType = CommandType.Text;
            command.CommandText = @"select * from phong";
            da.SelectCommand = command;
            da.Fill(dt);
            cbbsophong.DataSource = dt;
            cbbsophong.DisplayMember = "pid";
            cbbsophong.ValueMember = "pid";
        }

        private void cbbsophong_SelectedIndexChanged(object sender, EventArgs e)
        {
            dt1.Clear();
            int p = cbbsophong.SelectedIndex;
            SqlCommand command = new SqlCommand();
            command.Connection = conn;
            command.CommandType = CommandType.Text;
            command.CommandText = @"Select * from phong where pid =@pid";
            command.Parameters.Add("@pid", SqlDbType.Int, 20).Value = cbbsophong.Text;
            da.SelectCommand = command;
            da.Fill(dt1);
            showphong.DataSource = dt1;
            txttang.DataBindings.Clear();
            txttang.DataBindings.Add("Text", showphong.DataSource, "ptang");
            txtdientich.DataBindings.Clear();
            txtdientich.DataBindings.Add("Text",showphong.DataSource,"pdientich");
            txthuongnha.DataBindings.Clear();
            txthuongnha.DataBindings.Add("Text",showphong.DataSource,"phuongnha");
            txtthongtin.DataBindings.Clear();
            txtthongtin.DataBindings.Add("Text",showphong.DataSource,"pthongtin");
            anhphong.Image = imagephong.Images[p];

        }

        private void btnthoat_Click(object sender, EventArgs e)
        {
            Close();
            Dispose();
            quanlyphong qlp = new quanlyphong();
            qlp.Show();
        }

    }
}
